package com.yizhu.zczy.user.service.impl;

import com.yizhu.zczy.common.utils.PageUtils;
import com.yizhu.zczy.common.utils.Query;
import com.yizhu.zczy.user.entity.UserEntity;
import com.yizhu.zczy.user.service.UserService;
import com.yizhu.zczy.user.vo.NewMemberVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.yizhu.zczy.user.dao.UserlevelDao;
import com.yizhu.zczy.user.entity.UserlevelEntity;
import com.yizhu.zczy.user.service.UserlevelService;


@Service
public class UserlevelServiceImpl extends ServiceImpl<UserlevelDao, UserlevelEntity> implements UserlevelService {

    @Autowired
    private UserService userService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserlevelEntity> page = this.page(
                new Query<UserlevelEntity>().getPage(params),
                new QueryWrapper<UserlevelEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取昨天今天新增的会员
     * @return
     */
    @Override
    public List<NewMemberVo> getMembersAddYesterdayAndToday() {
        String yesterdayTime = LocalDateTime.of(LocalDate.now().minusDays(1), LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String nowTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        QueryWrapper<UserlevelEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("create_time",yesterdayTime,nowTime);
        List<UserlevelEntity> userlevelEntities = this.list(queryWrapper);
        List<NewMemberVo> newMemberVos = userlevelEntities.stream().map(item -> {
            NewMemberVo newMemberVo = new NewMemberVo();
            BeanUtils.copyProperties(item, newMemberVo);
            UserEntity userEntity = userService.getById(item.getUserId());
            BeanUtils.copyProperties(userEntity, newMemberVo);
            return newMemberVo;
        }).collect(Collectors.toList());
        return newMemberVos;
    }

}